package com.jingfu.Service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.jingfu.Service.DictService;
import com.jingfu.common.BadRequestException;
import com.jingfu.common.ErrorCode;
import com.jingfu.dto.DictDataDTO;
import com.jingfu.dto.DictTypeDTO;
import com.jingfu.mapper.SysDictDataMapper;
import com.jingfu.mapper.SysDictTypeMapper;
import com.jingfu.pojo.SysDictData;
import com.jingfu.pojo.SysDictType;
import com.jingfu.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author jingfuu@163.com
 * @version v1.0
 * @date 2022/2/9 上午9:26
 **/
@Slf4j
@Service
public class DictServiceImpl implements DictService {
    @Autowired
    SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    SysDictDataMapper sysDictDataMapper;

    @Override
    public List<DictTypeDTO> getDictTypeList(DictTypeDTO dictTypeDTO) {
        return sysDictTypeMapper.getDictTypeList(dictTypeDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dictTypeEnable(String id, String enable) {
        SysDictType dictType = sysDictTypeMapper.selectByPrimaryKey(id);
        if (Objects.isNull(dictType)) {
            throw new BadRequestException(ErrorCode.PARAM_IS_ERROR);
        }
        dictType.setEnable(enable);
        int i = sysDictTypeMapper.updateByPrimaryKey(dictType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDictType(String id) {
        SysDictType dictType = sysDictTypeMapper.selectByPrimaryKey(id);
        if (Objects.isNull(dictType)) {
            throw new BadRequestException(ErrorCode.PARAM_IS_ERROR);
        }
        dictType.setStatus("0");
        sysDictTypeMapper.updateByPrimaryKeySelective(dictType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dictTypeCreateOrUpdate(DictTypeDTO dictTypeDTO) {
        if (Objects.nonNull(dictTypeDTO.getId())) {
            //更新
            SysDictType dictType = sysDictTypeMapper.selectByPrimaryKey(dictTypeDTO.getId());
            if (Objects.isNull(dictType)) {
                throw new BadRequestException(ErrorCode.PARAM_IS_ERROR);
            }
            if (!StrUtil.equals(dictType.getTypeCode(), dictTypeDTO.getTypeCode())) {
                checkDictTypeCode(dictTypeDTO.getTypeCode());
            }
            dictType.setTypeCode(dictTypeDTO.getTypeCode());
            dictType.setDescription(dictTypeDTO.getDescription());
            dictType.setTypeName(dictTypeDTO.getTypeName());
            dictType.setUpdateTime(Date.from(Instant.now()));
            UserUtil.getCurrentUser().ifPresent(userDetail -> dictType.setUpdateBy(userDetail.getUsername()));
            sysDictTypeMapper.updateByPrimaryKeySelective(dictType);
            return;
        }
        //新增
        //校验权限标识不能重复
        checkDictTypeCode(dictTypeDTO.getTypeCode());
        SysDictType dictType = new SysDictType();
        BeanUtils.copyProperties(dictTypeDTO,dictType);
        dictType.setId(IdUtil.objectId());
        dictType.setCreateTime(Date.from(Instant.now()));
        UserUtil.getCurrentUser().ifPresent(userDetail -> dictType.setCreateBy(userDetail.getUsername()));
        dictType.setEnable("1");
        sysDictTypeMapper.insert(dictType);

    }

    @Override
    public List<DictDataDTO> getDictDataList(DictDataDTO dataDTO) {
        return sysDictDataMapper.getDictDataList(dataDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dictDataEnable(String id, String enable) {
        SysDictData dictData = sysDictDataMapper.selectByPrimaryKey(id);
        if (Objects.isNull(dictData)) {
            throw new BadRequestException(ErrorCode.PARAM_IS_ERROR);
        }
        dictData.setEnable(enable);
        int i = sysDictDataMapper.updateByPrimaryKey(dictData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDictData(String id) {
        SysDictData dictData = sysDictDataMapper.selectByPrimaryKey(id);
        if (Objects.isNull(dictData)) {
            throw new BadRequestException(ErrorCode.PARAM_IS_ERROR);
        }
        dictData.setStatus("0");
        sysDictDataMapper.updateByPrimaryKeySelective(dictData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dictDataCreateOrUpdate(DictDataDTO dictDataDTO) {
        if (Objects.nonNull(dictDataDTO.getDataId())) {
            //更新
            SysDictData dictData = sysDictDataMapper.selectByPrimaryKey(dictDataDTO.getDataId());
            if (Objects.isNull(dictData)) {
                throw new BadRequestException(ErrorCode.PARAM_IS_ERROR);
            }
            if (!StrUtil.equals(dictData.getDataValue(), dictDataDTO.getDataValue())) {
                checkDictDataValue(dictDataDTO.getDataValue());
            }
            dictData.setDataLabel(dictDataDTO.getDataLabel());
            dictData.setDataValue(dictDataDTO.getDataValue());
            dictData.setRemark(dictDataDTO.getRemark());
            dictData.setUpdateTime(Date.from(Instant.now()));
            UserUtil.getCurrentUser().ifPresent(userDetail -> dictData.setUpdateBy(userDetail.getUsername()));
            sysDictDataMapper.updateByPrimaryKeySelective(dictData);
            return;
        }
        //新增
        //校验字典值不能重复
        checkDictDataValue(dictDataDTO.getDataValue());
        SysDictData dictData = new SysDictData();
        BeanUtils.copyProperties(dictDataDTO,dictData);
        dictData.setDataId(IdUtil.objectId());
        dictData.setCreateTime(Date.from(Instant.now()));
        UserUtil.getCurrentUser().ifPresent(userDetail -> dictData.setCreateBy(userDetail.getUsername()));
        dictData.setEnable("1");
        sysDictDataMapper.insert(dictData);
    }

    private void checkDictDataValue(String dataValue) {
        SysDictData dictData = sysDictDataMapper.selectByDataValue(dataValue);
        if (Objects.nonNull(dictData)) {
            throw new BadRequestException(ErrorCode.DICT_VALUE_REPEATED);
        }

    }

    private void checkDictTypeCode(String typeCode) {
        SysDictType dictType = sysDictTypeMapper.selectByTypeCode(typeCode);
        if (Objects.nonNull(dictType)) {
            throw new BadRequestException(ErrorCode.DICT_CODE_REPEATED);
        }
    }
}
